package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
)

type IProdTagReferenceRepository interface {
	Creates(db *gorm.DB, tagList []*model.ProdTagReference) error
	FindTagIdByProdId(db *gorm.DB, prodId uint) ([]uint, error)
	DeleteBatch(db *gorm.DB, prodId uint) error
}

type ProdTagReferenceRepository struct {
}

func NewProdTagReferenceRepository() IProdTagReferenceRepository {
	return &ProdTagReferenceRepository{}
}

func (r *ProdTagReferenceRepository) Creates(db *gorm.DB, tagList []*model.ProdTagReference) error {
	return db.Create(&tagList).Error
}

func (r *ProdTagReferenceRepository) FindTagIdByProdId(db *gorm.DB, prodId uint) (tagList []uint, err error) {
	return tagList, db.Model(&model.ProdTagReference{}).Select("tag_id").Where("prod_id = ?", prodId).Find(&tagList).Error
}

func (r *ProdTagReferenceRepository) DeleteBatch(db *gorm.DB, prodId uint) error {
	return db.Where("prod_id = ?", prodId).Delete(&model.ProdTagReference{}).Error
}
